package ejb.facades.location;

import ejb.facades.AbstractFacade;
import entities.location.Country;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@Stateless
public class CountryFacade extends AbstractFacade<Country> implements CountryFacadeLocal {
    @PersistenceContext(unitName = "Trabalho_PDPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CountryFacade() {
        super(Country.class);
    }
    
    @Override
    public Country findByCountryName(String name) {
        List<Country> resultList = em.createNamedQuery("Country.findByCountryName", Country.class)
                .setParameter("countryName", name)
                .getResultList();
        if(resultList.isEmpty()) {
            return null;
        }
        return resultList.get(0);
    }
}
